#import "TimerGame.h"
#import "LabelGameComponent.h"
//#import "constants.h"

@interface TimerGame ()

@property(readwrite) double remainingTime;
@property NSInteger randomInterval;
@property BOOL isGameOver;

@end


@implementation TimerGame

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setName:@"TimerGame"];
        [self setType:reaction];
        self.ruleOfTheGame=@"Hit when countdown reaches 0";
    }
    return self;
}

-(BOOL)isRight{
    if (self.remainingTime>0) {
        return NO;
    
    }
    else{
        return YES;
    }
}

-(void)draw{
    LabelGameComponent* label=[[LabelGameComponent alloc]initWithText:@"" andFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:kScreenWidth/kWidthDel andColour:@"whiteColor"];
        LabelGameComponent* labelOne=[[LabelGameComponent alloc]initWithText:@"" andFrame:CGRectMake(0, kScreenHeight-kScreenHeight/kHeighDel - 2*kButtonHeigh, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:kScreenWidth/kWidthDel andColour:@"whiteColor"];

    [self addComponents:label];
    [self addComponents:labelOne];
}



-(void)play{
    [self draw];
    
    while (self.randomInterval < kTimerGameMinInterval) {
        self.randomInterval=arc4random_uniform(2* kTimerGameMinInterval);
        self.remainingTime=self.randomInterval+4;
    }
    
     [NSTimer scheduledTimerWithTimeInterval: 1                                                  target: self selector:@selector(warmUpCounter:)userInfo: nil repeats:YES];

}



-(void)onTick:(NSTimer *)timer {
    self.remainingTime=self.remainingTime-0.01;
    if (self.isGameOver==YES) {
        [timer invalidate];
    }
}




-(void)warmUpCounter:(NSTimer*)timer{
    if (self.isGameOver==YES) {
        [timer invalidate];
    }
    else{
        NSString* labelText;
        
        if (self.remainingTime > self.randomInterval) {
            labelText=[NSString stringWithFormat:@"%.0f",self.remainingTime];
            self.remainingTime=self.remainingTime-1;
        }
        else{
            labelText=[NSString stringWithFormat:@"?"];
            [timer invalidate];
            NSTimer *timer2 = [NSTimer scheduledTimerWithTimeInterval: 0.01                                                  target: self selector:@selector(onTick:)userInfo: nil repeats:YES];
            
        }
        
        
        NSMutableDictionary* dict=[[NSMutableDictionary alloc]init];
        [dict setValue:labelText forKey:@"info"];
        
        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"TimerGame"
         object:self userInfo:dict];

    }
}

-(void)gameOver{
    self.isGameOver=YES;
    NSString* labelText;
    labelText=[NSString stringWithFormat:@"%.3f",self.remainingTime];
    
    NSMutableDictionary* dict=[[NSMutableDictionary alloc]init];
    [dict setValue:labelText forKey:@"info"];
    
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"TimerGame"
     object:self userInfo:dict];
    
}


@end
